//1. 利用对象来存储数据，类似结构体
let myData = {};
myData.id = 1;
myData.name = "张三";
myData.age = 19;
document.writeln(`${myData.name}<br>`);

//2. Array对象
let arr = [1, 2, 3];
document.writeln(`arr:${arr}<br>`);

arr.push("a");
document.writeln(`arr:${arr}<br>`);
// arr.pop("a");
// document.writeln(`${arr}<br>`);

arr.unshift("b");
document.writeln(`arr:${arr}<br>`);
// arr.shift("b");
// document.writeln(`${arr}<br>`);

let newArr = [];
newArr = newArr.concat(arr, ["数组项"], 1, true, "x");
document.writeln(`newArr:${newArr}<br>`);

newArr = arr.slice(0, 2);  //切片
document.writeln(`newArr:${newArr}<br>`);

arr.splice(0, 2);  //删除  第0个开始，删除2个
document.writeln(`arr:${arr}<br>`);

arr.splice(0, 0, "c", 0);  //插入：新增（insert）
document.writeln(`arr:${arr}<br>`);

arr.splice(2, 2, 5, 4,3,3);  //替换（replace）即 updata
document.writeln(`arr:${arr}<br>`);

document.writeln(`查找返回索引值:${arr.indexOf(3)}<br>`);
document.writeln(`查找返回索引值:${arr.lastIndexOf(3)}<br>`);

document.writeln(`排序:${arr.sort()}<br>`);
document.writeln(`反序:${arr.reverse()}<br>`);

let arr2 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
document.writeln(`arr2:${arr2}<br>`);
let sum = 0;
arr2.forEach(x => (sum += x));
document.writeln(`求和:${sum}<br>`);

let newArr2 = arr2.map(x => x * 2);
document.writeln(`arr2*2:${newArr2}<br>`);

document.writeln(`过滤:${arr2.filter((x) => x % 2 === 0)}<br>`);

document.writeln(`查找:${arr2.find((x) => x % 2 === 0)}<br>`);
document.writeln(`查找索引值:${arr2.findIndex((x) => x % 2 === 0)}<br>`);

document.writeln(`查找:${arr2.every((x) => x > 0)}<br>`);
document.writeln(`查找:${arr2.some((x) => x % 2 === 0)}<br>`);

document.writeln(`依据回调函数进行归并：${arr2.reduce((x, y) => x + y, 0)}<br>`);

//3. Map  key-value的集合
let myMap = new Map();
document.writeln(`Map集合:${myMap.size}<br>`);

myMap.set("id", 1);
document.writeln(`Map集合:${myMap.size}<br>`);

myMap.set("name", "李四");
document.writeln(`Map元素:${myMap.get("name")}<br>`);

document.writeln(`Map元素:${myMap.entries()}<br>`);
document.writeln(`Map键:${myMap.keys()}<br>`);
document.writeln(`Map值:${myMap.values()}<br>`);

for (const value of myMap.values()) {
    document.writeln(`Map(values)迭代:${value}<br>`);
}

myMap.delete("id");
document.writeln(`Map元素:${myMap.has("id")}<br>`);

myMap.clear();
document.writeln(`Map集合:${myMap.size}<br>`);

//4. Set  唯一值的集合
let mySet = new Set();
document.writeln(`Set集合:${mySet.size}<br>`);
mySet.add(0);
mySet.add("a");
document.writeln(`Set集合:${mySet.size}<br>`);
for (const value of mySet.values()) {
    document.writeln(`Set(values)迭代:${value}<br>`);
}
mySet.delete("a")
document.writeln(`Set集合:${mySet.size}<br>`);

mySet.clear();
document.writeln(`Set集合:${mySet.size}<br>`);

//5 JSON对象  结构化的对象 布尔，数值，字符串，JSONObject"{}" JSONArray"[]"
let myJSON = {
    id: 1,
    name: "王五",
    score: {
        s1: 100,
        s2: 97,
        s3: 96
    }
};

//传输的时候同是JSON字符串
let myJSONStr = JSON.stringify(myJSON);
document.writeln(`JSON字符串:${myJSONStr}<br>`);
document.writeln(typeof myJSONStr);
document.writeln(`<br>`);

//JSON字符串转换JSON对象
let myJSONObject = JSON.parse(myJSONStr);
document.writeln(`${myJSONObject.score.s1}<br>`);
document.writeln(`${myJSONObject.score.s2}<br>`);
document.writeln(`${myJSONObject.score.s3}<br>`);

document.writeln(`<br>`);
document.writeln(`<br>`);
document.writeln(`<br>`);
document.writeln(`<br>`);